package l1j.server.server.serverpackets;

import l1j.server.server.datatables.NPCTalkDataTable;
import l1j.server.server.model.L1NpcTalkData;
import l1j.server.server.model.Instance.L1FieldObjectInstance;
import l1j.server.server.model.Instance.L1NpcInstance;

// Referenced classes of package l1j.server.server.serverpackets:
// ServerBasePacket

public class S_NPCPack extends ServerBasePacket {

	private static final String S_NPC_PACK = "[S] S_NPCPack";
	private static final int STATUS_POISON = 1;
	private byte[] _byte = null;

	public S_NPCPack(final L1NpcInstance npc) {
		this.writeC(S_OPCODE_CHARPACK);
		this.writeH(npc.getX());
		this.writeH(npc.getY());
		this.writeD(npc.getId());
		if (npc.getTempCharGfx() == 0) {
			this.writeH(npc.getGfxId());
		} else {
			this.writeH(npc.getTempCharGfx());
		}
		/*
		 * if (npc.getNpcTemplate().is_doppel() && npc.getGfxId() != 31) { //
		 * スライムの姿をしていなければドッペル writeC(4); // 長剣 } else { writeC(npc.getStatus());
		 * }
		 */
		// TODO 修正變形怪變身幻術師變成空手攻擊by9001183ex
		if (npc.getNpcTemplate().getAttStatus() != 0) {
			this.writeC(npc.getNpcTemplate().getAttStatus());
		} else {
			this.writeC(npc.getStatus());
		}
		// TODO 修正變形怪變身幻術師變成空手攻擊by9001183ex

		this.writeC(npc.getHeading());
		this.writeC(npc.getChaLightSize());
		this.writeC(npc.getMoveSpeed());
		this.writeD((int) npc.getExp());
		this.writeH(npc.getTempLawful());
		this.writeS(npc.getNameId());// opqlo 屬性顯示

		if (npc instanceof L1FieldObjectInstance) { // SICの壁字、看板など
			final L1NpcTalkData talkdata = NPCTalkDataTable.getInstance().getTemplate(npc.getNpcTemplate().get_npcId());
			if (talkdata != null) {
				this.writeS(talkdata.getNormalAction()); // タイトルがHTML名として解釈される
			} else {
				this.writeS(null);
			}
		} else {
			this.writeS(npc.getTitle());
		}

		/**
		 * シシニテ - 0:mob,item(atk pointer), 1:poisoned(), 2:invisable(), 4:pc,
		 * 8:cursed(), 16:brave(), 32:??, 64:??(??), 128:invisable but name
		 */
		int status = 0;
		if (npc.getPoison() != null) { // 毒状態
			if (npc.getPoison().getEffectId() == 1) {
				status |= STATUS_POISON;
			}
		}
		// TODO 變怪免強制攻擊 by 0919162173
		/*
		 * 刪除 if (npc.getNpcTemplate().is_doppel()) { //
		 * PC属性だとエヴァの祝福を渡せないためWIZクエストのドッペルは例外 if
		 * (npc.getNpcTemplate().get_npcId() != 81069) { status |= STATUS_PC; }
		 * } 刪除
		 */
		this.writeC(status);

		this.writeD(0); // 0以外にするとC_27が飛ぶ
		this.writeS(null);
		this.writeS(null); // マスター名？
		this.writeC(0);
		this.writeC(0xFF); // HP
		this.writeC(0);
		this.writeC(npc.getLevel());
		this.writeC(0);
		this.writeC(0xFF);
		this.writeC(0xFF);
	}

	public S_NPCPack(final L1NpcInstance npc, final boolean gm) {
		this.writeC(S_OPCODE_CHARPACK);
		this.writeH(npc.getX());
		this.writeH(npc.getY());
		this.writeD(npc.getId());
		if (npc.getTempCharGfx() == 0) {
			this.writeH(npc.getGfxId());
		} else {
			this.writeH(npc.getTempCharGfx());
		}
		/*
		 * if (npc.getNpcTemplate().is_doppel() && npc.getGfxId() != 31) { //
		 * スライムの姿をしていなければドッペル writeC(4); // 長剣 } else { writeC(npc.getStatus());
		 * }
		 */
		// TODO 修正變形怪變身幻術師變成空手攻擊by9001183ex
		if (npc.getNpcTemplate().getAttStatus() != 0) {
			this.writeC(npc.getNpcTemplate().getAttStatus());
		} else {
			this.writeC(npc.getStatus());
		}
		// TODO 修正變形怪變身幻術師變成空手攻擊by9001183ex

		this.writeC(npc.getHeading());
		this.writeC(npc.getChaLightSize());
		this.writeC(npc.getMoveSpeed());
		this.writeD((int) npc.getExp());
		this.writeH(npc.getTempLawful());
		final StringBuilder buff = new StringBuilder();
		buff.append(npc.getNameId()).append("[").append(npc.getNpcId()).append("]\\fC圖檔[").append(npc.getGfxId())
		.append("]\\f2面向[").append(npc.getHeading()).append("]");
		this.writeS(buff.toString());// opqlo 屬性顯示(GM顯示NPCID)

		if (npc instanceof L1FieldObjectInstance) { // SICの壁字、看板など
			final L1NpcTalkData talkdata = NPCTalkDataTable.getInstance()
					.getTemplate(npc.getNpcTemplate().get_npcId());
			if (talkdata != null) {
				this.writeS(talkdata.getNormalAction()); // タイトルがHTML名として解釈される
			} else {
				this.writeS(null);
			}
		} else {
			this.writeS(npc.getTitle());
		}

		/**
		 * シシニテ - 0:mob,item(atk pointer), 1:poisoned(), 2:invisable(), 4:pc,
		 * 8:cursed(), 16:brave(), 32:??, 64:??(??), 128:invisable but name
		 */
		int status = 0;
		if (npc.getPoison() != null) { // 毒状態
			if (npc.getPoison().getEffectId() == 1) {
				status |= STATUS_POISON;
			}
		}
		// TODO 變怪免強制攻擊 by 0919162173
		/*
		 * 刪除 if (npc.getNpcTemplate().is_doppel()) { //
		 * PC属性だとエヴァの祝福を渡せないためWIZクエストのドッペルは例外 if
		 * (npc.getNpcTemplate().get_npcId() != 81069) { status |= STATUS_PC; }
		 * } 刪除
		 */
		this.writeC(status);
		this.writeD(0); // 0以外にするとC_27が飛ぶ
		this.writeS(null);
		this.writeS(null); // マスター名？
		this.writeC(0);
		this.writeC(0xFF); // HP
		this.writeC(0);
		this.writeC(npc.getLevel());
		this.writeC(0);
		this.writeC(0xFF);
		this.writeC(0xFF);
	}

	@Override
	public byte[] getContent() {
		if (this._byte == null) {
			this._byte = this._bao.toByteArray();
		}

		return this._byte;
	}

	@Override
	public String getType() {
		return S_NPC_PACK;
	}

}
